package com.FH.permission.sys_role;

import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by acer on 2016/8/1.
 */
@Stateless
public class SysroleDAOImpl extends AccessDB implements SysroleDAO {
    /**
     * 获取角色信息
     *
     * @param search
     * @param page
     * @param size
     * @return
     */

    public ResultDTO getSysroleInfo(String search, String page, String size) {

        ResultDTO resultDTO = new ResultDTO();

        try {
            JSONObject jsonObject = new JSONObject(search);

            String sql_w = "";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);//将定义page、size的String类型转换成Integer类型
            Integer size_ok = Integer.parseInt(size);

            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok + 1) + "," + size + "";
            }


            if (jsonObject.has("sys_role_id")) {//角色id
                sql_w += " AND sys_role_id =" + jsonObject.getString("sys_role_id");
            }

            String sqlexit = "select count(1) from  sys_role  where 1=1 ";
            String sqlexit_ok = sqlexit + sql_w;

            String existName = getReportingToOne(sqlexit_ok);//查询此数据是否存在

            List<String[]> seresult = new ArrayList<String[]>();//存储查询到的结果

            List<SysroleShowDTO> result = new ArrayList<SysroleShowDTO>();

            if (!existName.equals("0")) {

                String sql = "select sys_role_id,sys_role_name,sys_role_desc,sys_role_create,sys_role_create_time,sys_role_modify," +
                        "sys_role_modify_time from sys_role where 1=1" + sql_w + sql_page;
                seresult = this.getReportingTo(sql);

            }

            if (seresult.size() > 0) {

                for (int i = 0; i < seresult.size(); i++) {
                    SysroleShowDTO sysroleShowDTO = new SysroleShowDTO();

                    sysroleShowDTO.setSys_role_id(seresult.get(i)[1]);
                    sysroleShowDTO.setSys_role_name(seresult.get(i)[2]);
                    sysroleShowDTO.setSys_role_desc(seresult.get(i)[3]);
                    sysroleShowDTO.setSys_role_create(seresult.get(i)[4]);
                    sysroleShowDTO.setSys_role_create_time(seresult.get(i)[5]);
                    sysroleShowDTO.setSys_role_modify(seresult.get(i)[6]);
                    sysroleShowDTO.setSys_role_modify_time(seresult.get(i)[7]);

                    result.add(sysroleShowDTO);
                }
                resultDTO.setValues(result);
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }


        return resultDTO;
    }

    @Override
    public ResultDTO addSysroleInfo(SysrolePullDTO dataDTO) {
        int resultls = 0;
        ResultDTO resultDTO = new ResultDTO();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        if (dataDTO.getSys_role_name() != null) {
            String sys_role_name = dataDTO.getSys_role_name();

            String sql = "select * from sys_role where sys_role_name='" + sys_role_name + "'";
            String sum = getReportingToOne(sql);
            if (!sum.equals("")) {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion("角色已存在请重新分配角色");
                return resultDTO;
            }
        }

        String Orgrole_insert = "";
        Orgrole_insert = "insert into sys_role (sys_role_id,sys_role_name,sys_role_desc,sys_role_create,sys_role_create_time,sys_role_modify,sys_role_modify_time)" +
                "values('" + uuid + "','" + dataDTO.getSys_role_name() + "','" + dataDTO.getSys_role_desc() + "','" + dataDTO.getSys_role_create() + "'" +
                ",'" + df.format(new Date()) + "','" + dataDTO.getSys_role_modify() + "','" + df.format(new Date()) + "')";

        resultls = addReportingTo(Orgrole_insert);
        if (resultls != 0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.add_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }

    @Override
    public ResultDTO deleteSysroleInfo(SysrolePullDTO dataDTO) {

        //删除之前查询一下该表是否有记录
        String sql = "select count(1) from sys_role where sys_role_id='" + dataDTO.getSys_role_id() + "'";

        String existName = getReportingToOne(sql);

        int result = 0;

        if (!existName.equals(0)) {

            String QUERY = "";
            QUERY += "delete from sys_role where sys_role_id='" + dataDTO.getSys_role_id() + "'";

            result = this.addReportingTo(QUERY);
        }

        ResultDTO resultDTO = new ResultDTO();
        if (result != 0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);

        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);
        }
        return resultDTO;
    }

}
